<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/Control.php');
require_once($Config->WebCMSRoot.'Models/Form.php');
require_once($Config->WebCMSRoot.'Models/Ticket.php');
require_once($Config->WebCMSRoot.'Views/Ticket.php');

Class Ticket extends BaseControl
{
	var $ticket;
	var $view;
	var $CustomCode;

	function Ticket($parameters, &$page)
	{
		$this->ticket = new TicketModel();
		$this->view = new TicketView();

		parent::BaseControl($parameters, &$page);
	}

	function Execute()
	{
	  switch (($this->GetParameter('action')))
	  {
	    case 'code':
    		if (isset($_POST['formID']) && isset($_SESSION[$_POST['formID']]) && $_SESSION[$_POST['formID']]->IsComplete )
	      {
	        $this->parameters['code'] = $_POST['formCode'];
      		$this->ticket->Retrieve(&$this->parameters);

       		if (count($this->ticket->Collection) > 0)
       		{
       		  // Ticket already registered
       		  // Do nothing for now
        		//$this->view->ShowTicketAlreadyRegistered();
       		}
       		else
       		{
    	      $this->ticket->Name = $this->ExtractFormParameter($this->GetParameter('name'));
    	      $this->ticket->Address = $this->ExtractFormParameter($this->GetParameter('address'));
    	      $this->ticket->City = $this->ExtractFormParameter($this->GetParameter('city'));
    	      $this->ticket->Email = $this->ExtractFormParameter($this->GetParameter('email'));
  
    	      $amounts = explode('|', $this->GetParameter('amount'));
    	      $prices = explode('|', $this->GetParameter('price'));
    	      
    	      $this->ticket->Price = 0;
    	      foreach($prices as $key=>$price)
    	      {
    	        $this->ticket->Price += ($prices[$key] * $this->ExtractFormParameter($amounts[$key]));
    	      }
  
    	      $this->ticket->FormID = $_SESSION[$_POST['formID']]->ID;
    	      $this->ticket->Created = date('Y-m-d H:i:s');
  
            $this->ticket->Persist();
          }
  	    }
	      break;
	      
	    case 'confirm':
	      if ($this->ParameterExists('code') && $this->GetParameter('code') != '')
	      {
	        // Confirmation link
	        $this->ProcessConfirmationCode();
	      }
	      else
	      {
	        // Enter Confirmation code
	        if (isset($_POST['code']))
	        {
	          $this->parameters['code'] = $_POST['code'];
  	        $this->ProcessConfirmationCode();
	        }
	        else
	        {
        		$this->view->ShowConfirmationCodeForm();
  	      }
	      }
        break;
    }

		parent::Execute();
	}

	function ValidateArguments()
	{
		$valid = (count($this->parameters) >= 1);
		
		switch (true)
		{
			case ($this->ParameterExists('action')):
				$valid = true;
				break;
				
			default:
				$valid = false;
		}

		return $valid;
	}
	
	/* Extended Functions */
	function ExtractFormParameter($parameter)
	{
		$form = $_SESSION[$_POST['formID']];
		$result = preg_match_all('|^FORM:(?P<field>\w*)|i', $parameter, $matches, PREG_SET_ORDER);

		if ($result >= 1)
		{
			$value = $form->FormFields->Collection[$matches[0]['field']]->Value;
		}
		else
		{
			if ($this->ParameterExists($parameter))
			{
			  $value = $this->GetParameter($parameter);
			}
			else
			{
			  $value = $parameter;
			}
    }
		
		return $value;
	}
	
	function ProcessConfirmationCode()
	{
		$this->ticket->Retrieve(&$this->parameters);
		
		if (count($this->ticket->Collection) == 1)
		{
		  // Confirm
		  if ($this->ticket->Collection[0]->Confirmed != '0000-00-00 00:00:00')
		  {
    		$this->view->ShowTicketAlreadyConfirmed();
      }
      else
      {
    		$this->ticket->Collection[0]->Confirmed = date('Y-m-d H:i:s');
    		$this->ticket->Collection[0]->Persist();
    		
    		$this->view->ShowConfirmationSuccess();
    	}
  	}
  	else
  	{
  		$this->view->ShowNoConfirmationExists();
    }
	}
}
?>